
import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);
// import createPersistedState from 'vuex-persistedstate'


const store = new Vuex.Store({
    // plugins:[new createPersistedState({
    //     storage:window.sessionStorage
    // })],
    state: {
        // 初始化状态
        count: 0,
        feature:[
            'aa','bb','cc','dd','index'
        ],
        choices: [],  //保存各题号及其对应的选项
        scorePerQues: 0,
        score: 0
    },
    mutations: {
        // 处理状态
        increment(state, payload) {
            state.count += payload.step || 1;
        },
        updateRecord(state, choice) {
            var flag = false
            if (state.choices.length === 0) {
              state.choices.push(choice)
              flag = true
              return true
            }
            state.choices.forEach(item => {
              if (item.quesIndex === choice.quesIndex) {
                item.choiceIndex = choice.choiceIndex
                item.correctness = choice.correctness
                flag = true
                return true
              }
            })
            if (!flag) {
              state.choices.push(choice)
              return true
            }
            return false
          },
          getScore(state) {
            state.choices.forEach(item => {
              if (item.correctness === true) {
                state.score += state.scorePerQues
              }
            })
          },
          //重置state中的所有值，重新答题的时候触发
          resetStore(state) {
            state.choices = []
            state.scorePerQues = 0
            state.score = 0
          }
    },
    actions: {
        // 提交改变后的状态
        increment(context, param) {
            context.state.count += param.step;
            context.commit('increment', context.state.count)//提交改变后的state.count值
        },
        incrementStep({state, commit, rootState}) {
            if (rootState.count < 100) {
                store.dispatch('increment', {//调用increment()方法
                    step: 10
                })
            }
        }
    }
});

export default store;